package com.chc.web.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chc.web.menu.entity.MakeMenuTree;
import com.chc.web.menu.entity.Menu;
import com.chc.web.menu.mapper.MenuMapper;
import com.chc.web.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2021/12/22/23:27
 * @Description:
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getList() {
        //构造查询条件
        QueryWrapper<Menu> query = new QueryWrapper<>();
        query.lambda().orderByAsc(Menu::getName);
        List<Menu> menus = menuMapper.selectList(query);
        //组装成树数据
        List<Menu> menus1 = MakeMenuTree.makeTree(menus,0L);
        return menus1;
    }

    @Override
    public List<Menu> getParentList() {
        //上级菜单只需查询目录和菜单
        String[] types ={"0","1"};
        //构造查询条件
        QueryWrapper<Menu> query = new QueryWrapper();
        query.lambda().in(Menu::getType, Arrays.asList(types)).orderByAsc(Menu::getOrderNum);
        List<Menu> menus = menuMapper.selectList(query);
        //构造顶级菜单
        Menu menu = new Menu();
        menu.setMenuId(0L);
        menu.setParentId(-1L);
        menu.setMenuLabel("顶级菜单");
        menus.add(menu);

        //构造菜单树
        List<Menu> menus1 = MakeMenuTree.makeTree(menus,-1L);
        return menus1;
    }

    @Override
    public List<Menu> getMenuListByUserId(Long userId) {
        return this.baseMapper.getMenuListByUserId(userId);
    }

    @Override
    public List<Menu> getMenuListByUserIdForLiveUser(Long userId) {
        return this.baseMapper.getMenuListByUserIdForLiveUser(userId);
    }

    @Override
    public List<Menu> getMenuListByRoleId(Long roleId) {
        return this.baseMapper.getMenuListByRoleId(roleId);
    }
}
